package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.page(setmealPageQueryDTO);
        long total = page.getTotal();
        List<SetmealVO> rows = page.getResult();
        return new PageResult(total,rows);
    }
    @Transactional
    @Override
    public void add(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        // 拷贝setmealDTO到setmeal
        BeanUtils.copyProperties(setmealDTO,setmeal);
        // 添加套餐
        setmealMapper.add(setmeal);
        // 添加套餐菜品关系
        Long id = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        // 为套餐菜品关系设置套餐id
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(id);
        }
        setmealDishMapper.insertBatch(setmealDishes);
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        //创建一个setmeal对象
        Setmeal setmeal = new Setmeal();
        // 拷贝setmealDTO到setmeal
        BeanUtils.copyProperties(setmealDTO,setmeal);
        // 更新套餐对数据库操作由于缺少字段所以要在mapper层加自动填充字段注解
        setmealMapper.update(setmeal);
        // 添加套餐菜品关系获取套餐菜品集合
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //如果要删除菜品前端调用这个删除套餐中菜品的mapper
        setmealDishMapper.deleteByDishId(setmealDTO.getId());
        //批量增加套餐中的菜品
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDTO.getId());
        }
        setmealDishMapper.insertBatch(setmealDishes);
    }

    @Override
    public SetmealVO getById(Long id) {
        //由于前端要的是不只有套餐的信息还有菜品信息所以我们要封装一个vo对象传递给前端
        SetmealVO setmealVO =new SetmealVO();
        // 拷贝套餐封装对象的信息
        Setmeal setmeal = setmealMapper.getById(id);
        BeanUtils.copyProperties(setmeal,setmealVO);
        // 查询套餐菜品关系并得到有关该套餐的菜品集合
        List<SetmealDish> setmealDishes = setmealDishMapper.getSetmealIdsByDishId(id);
        // 封装套餐菜品集合到vo对象中
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }


    //加事事务如果批量删除删除一个失败其他的也失败
    @Transactional
    @Override
    // 删除套餐如果启售中的套餐不可删除
    //批量删除套餐如果一个失败删除其他的也失败事务回滚
    public void delete(List<Long> ids) {
        for (Long id : ids){
            if (setmealMapper.getById(id).getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
            else {
                setmealMapper.delete(id);
                setmealDishMapper.deleteByDishId(id);
            }
        }
    }

    @Override
    public void startOrStop(Integer status, Long id) {
       if (status == StatusConstant.ENABLE){
           List<Dish> dishes= setmealDishMapper.getById(id);
           for (Dish dish : dishes) {
               if (dish.getStatus() == StatusConstant.DISABLE){
                   throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
               }
           }
           Setmeal setmeal = new Setmeal();
           setmeal.setId(id);
           setmeal.setStatus(status);
           setmealMapper.update( setmeal);
       }

    }


}
